<template>
  <div class="white-box">
    <div class="table-mb">
      <el-button type="primary" @click="handleAdd">添加大区</el-button>
    </div>

    <div class="task-one">
      <div class="task-one__item">
        <span class="task-one__label">大区名称：</span>
        <el-input placeholder="请输入大区名称" v-model="searchParams.area_title" @keyup.enter.native="handleSearch"
                  clearable></el-input>
      </div>
      <div class="task-one__item">
        <span class="task-one__label">负责人姓名：</span>
        <el-input placeholder="请输入负责人姓名" v-model="searchParams.area_fzr_name" @keyup.enter.native="handleSearch"
                  clearable></el-input>
      </div>
      <div class="task-one__item">
        <span class="task-one__label">负责人手机号：</span>
        <el-input placeholder="请输入负责人手机号" v-model="searchParams.area_fzr_tel" oninput="value=value.replace(/[^\d]/g,'')" @keyup.enter.native="handleSearch"
                  clearable></el-input>
      </div>
      <div class="task-one__item">
        <span class="task-one__label">创建时间：</span>
        <el-date-picker
            v-model="searchParamsTime"
            type="datetimerange"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
            date-format="YYYY/MM/DD ddd"
            time-format="A hh:mm:ss"
        />
      </div>
      <div class="task-one__btn clearfix">
        <el-button type="primary" :loading="searchBtnLoading" :icon="Search" @click="handleSearch">搜索</el-button>
      </div>
    </div>

    <el-table class="table-mb" v-loading="loading" :data="list" :header-cell-style="{ background: '#F5F7FA' }"
              element-loading-background="rgba(255, 255, 255, 0.6)">
      <el-table-column prop="area_title" label="大区名称"></el-table-column>
      <el-table-column prop="area_fzr_name" label="负责人姓名"></el-table-column>
      <el-table-column prop="area_fzr_tel" label="负责人手机号"></el-table-column>
      <el-table-column prop="shop_num" label="门店数量"></el-table-column>
      <el-table-column prop="waiy_pay_total_money" label="待结算佣金"></el-table-column>
      <el-table-column prop="created_at" label="创建时间"></el-table-column>
      <el-table-column label="操作" width="200" fixed="right">
        <template #default="{ row }">
          <el-button link type="primary" @click="handleEdit(row,{area_id:row.area_id})">编辑</el-button>
          <el-button link type="primary" @click="handleShop(row)">门店列表</el-button>
          <el-popconfirm :persistent="false" title="确定删除吗？" confirm-button-text="确定" cancel-button-text="取消"
                         @confirm="handleDelete(row)">
            <template #reference>
              <el-button link type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination background v-model:current-page="page" v-model:page-size="pageSize" :page-sizes="pageSizeNum"
                   :total="totalNums" layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>


    <el-dialog v-model="dialogShow" :title="dialogParams.type == 'add'?'添加':'编辑'" width="660px" :append-to-body="true" :destroy-on-close="true"
               :close-on-click-modal="false" @close="closeDialog">
      <el-form ref="dialogFormRef" :model="dialogForm" :rules="dialogFormRules" label-width="120px">

        <el-form-item label="大区名称" prop="area_title" required>
          <el-input v-model.trim="dialogForm.area_title" clearable placeholder="请输入大区名称"/>
        </el-form-item>
        <el-form-item label="负责人姓名" prop="area_fzr_name" required>
          <el-input v-model.trim="dialogForm.area_fzr_name" clearable placeholder="请输入负责人姓名"/>
        </el-form-item>
        <el-form-item label="负责人手机号" prop="area_fzr_tel" required>
          <el-input v-model.trim="dialogForm.area_fzr_tel" clearable placeholder="请输入负责人手机号" oninput="value=value.replace(/[^\d]/g,'')"/>
        </el-form-item>

        <el-form-item>
          <div class="flexs">
            <el-button type="primary" :loading="dialogParams.loading" @click="submitDialogForm(dialogFormRef)">
              确认
            </el-button>
            <el-button @click="resetDialogForm(dialogFormRef)">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>

    <go-back/>
  </div>
</template>

<script setup>
import {
  getCurrentInstance,
  ref,
  reactive,
  onMounted,
  watch
} from "vue"
import listMixins from "@mixins/list"
import {
  useRouter
} from "vue-router"
import {
  Search
} from "@element-plus/icons-vue"
import GoBack from "@components/GoBack.vue"

const router = useRouter()
const {
  proxy
} = getCurrentInstance()

/**
 * minxin列表加载
 */
const {
  page,
  pageSizeNum,
  pageSize,
  loading,
  totalNums,
  searchParams,
  searchBtnLoading,
  apiPath,
  list,
  dialogFormRef,
  dialogShow,
  dialogForm,
  dialogParams,
  handleSearch,
  getData,
  handleAdd,
  handleEdit,
  resetDialogForm,
  handleAllSelected,
  submitDialogForm,
  closeDialog,
  handleAction
} = listMixins()

dialogForm.area_title = '';
dialogForm.area_fzr_name = '';
dialogForm.area_fzr_tel = '';

apiPath.value = 'admin/area/area/index';
dialogParams.addApiPath = 'admin/area/area/create';
dialogParams.editApiPath = 'admin/area/area/update';

onMounted(() => {
  getData()
})
const searchParamsTime = ref('');
watch(searchParamsTime,(newV, oldV)=>{
  if(!newV){
    searchParams.s_time = ''
    searchParams.e_time = ''
  }else{
    searchParams.s_time = newV[0]
    searchParams.e_time = newV[1]
  }
})

const dialogFormRules = reactive({
  area_title: [{
    required: true,
    message: "请输入大区名称",
    trigger: "blur"
  }],
  area_fzr_name: [{
    required: true,
    message: "请输入负责人姓名",
    trigger: "blur"
  }],
  area_fzr_tel: [{
    required: true,
    message: "请输入负责人手机号",
    trigger: "blur"
  },{
    validator: proxy.$util.validPhone,
    trigger: "blur"
  }]
})


const handleDelete=(row)=> {
  handleAction('admin/area/area/delete', {
    area_id: row.area_id,
  })
}

const handleShop = (row) => {
  router.push({
    path: "/shop/shop",
    query: {
      area_id: row.area_id,
      metaTitle:`【${row.area_title}】门店列表`
    },
  })
}

</script>

<style lang="less">

</style>